<template>
    <div>
        <el-form class="form-box" ref="formRef" :model="form" label-width="auto" size="default">
            <el-row>
                <el-col :span="24">
                    <el-form-item label="收款金额：" prop="salaAmt">
                        <div class="salaAmt">{{ unpay }}</div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="挂账人：" prop="mountUser">
                        <el-input v-model="form.mountUser" placeholder="挂账人" @focus="focusForm('mountUser')"
                            @blur="blurForm" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="性别：" prop="mountSex" label-width="80px">
                        <el-radio-group v-model="form.mountSex">
                            <el-radio value="011001">男</el-radio>
                            <el-radio value="011002">女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="手机：" prop="mountMobile">
                        <el-input v-model="form.mountMobile" placeholder="请输入手机号" @focus="focusForm('mountMobile')"
                            @blur="blurForm" clearable />
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="担保人：" prop="suretyUser">
                        <el-select v-model="form.suretyUser" filterable>
                            <el-option v-for="item in suretyUser_options" :key="item.userCode" :value="item.userCode"
                                :label="item.userName" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="挂账金额：" prop="payAmt">
                        <el-input v-model="form.payAmt" placeholder="请输入金额" @focus="focusForm('payAmt')" @blur="blurForm"
                            clearable oninput="value=value.replace(/[^0-9.]/g,'')"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script setup >
import ls from '@/lib/ls'
import { formatDecimal } from '@/utils/chenhao'
import Api from '@/api'
import useConfigStore from '@/store/config'
const configStore = useConfigStore()
// 接收父组件参数 props
const props = defineProps({
    tableInfo: {
        type: Object,
        required: false,
    },
    selectWares: {
        type: Array,
        required: false,
    },
    received: {
        type: String,
        required: false,
    },
    form: {
        type: Object,
        required: false,
    },
    price: {
        type: String,
        required: false,
    },
    unpay: {
        type: String,
        required: false,
    },
    payData: {
        type: Array,
        required: false,
    },
    saleData: {
        type: Array,
        required: false,
    },
    details: {
        type: Array,
        required: false,
    },
})
const tableInfo = toRef(props, 'tableInfo')
const selectWares = toRef(props, 'selectWares')
const received = toRef(props, 'received')
const price = toRef(props, 'price')
const unpay = toRef(props, 'unpay')
const form = toRef(props, 'form')
const payData = toRef(props, 'payData')
const saleData = toRef(props, 'saleData')
const details = toRef(props, 'details')


const emit = defineEmits(['focusForm'])
// 数字键盘
const focusForm = (str) => {
    // keyboard.value = true
    emit('focusForm', str)
}
const blurForm = (params) => {
    // keyboard.value = false
}

// 获取挂账人列表OPTIONS
let suretyUser_options = ref([])
const get_suretyUser_options = (params) => {
    suretyUser_options.value = configStore.waiterList
}
get_suretyUser_options()


// 父组件小键盘点击确定
const handleKeyboardConfirm = (params) => { 
    if (!form.value.mountUser) return ElNotification({
        title: '提示',
        message: '请输入挂账人',
        type: 'warning',
    })
    if (!form.value.mountMobile) return ElNotification({
        title: '提示',
        message: '请输入手机号',
        type: 'warning',
    })
    if (!form.value.suretyUser) return ElNotification({
        title: '提示',
        message: '请选择担保人',
        type: 'warning',
    })
    if (unpay.value <= 0) return ElNotification({
        title: '提示',
        message: '收款金额为0无法收款',
        type: 'warning',
    })
    if (Number(form.value.payAmt) > Number(unpay.value)) return ElNotification({
        title: '提示',
        message: '收取金额不能大于收款金额',
        type: 'warning',
    })
    if (!form.value.payAmt) return ElNotification({
        title: '提示',
        message: '请输入收取金额',
        type: 'warning',
    })
    configStore.payFlag = true // 支付开关
    // 处理勾选的商品
    let detailList = []
    if (details.value.length > 0) {
        console.log('1 :>> ', 1);
        detailList = details.value
    } else {
        console.log('2 :>> ', 2);
         console.log(selectWares.value);
        let toolArr = []
        selectWares.value.forEach(item=>{
            toolArr.push(item)
            if (item.subGoods && item.subGoods.length > 0) {
                toolArr.push(...item.subGoods)
            }
        })
   
        console.log(toolArr);
        
        detailList = toolArr.map(item => {
            return item.itemCode
        })
    }
    const obj = {
        zffs: form.value.actionType,
        skje: formatDecimal(form.value.payAmt),
        payType: '008',
        consumeCode: form.value.consumeCode,
        totalAmt: unpay.value,
        payAmt: form.value.payAmt,
        mountUser: form.value.mountUser,
        mountSex: form.value.mountSex,
        mountMobile: form.value.mountMobile,
        suretyUser: form.value.suretyUser,
        detailList,
    }
    const data = {
        payType: '008',
        consumeCode: tableInfo.value.consumeCode,
        roomCode: tableInfo.value.roomCode,
        payBatchCode: tableInfo.value.payBatchCode,
        orderAmt: price.value,
        realPayAmt: form.value.payAmt,
        detailList: detailList.join(','),
        payList: [obj],
        serviceAmt: saleData.value
    }
    Api.Common.PcObjProcess.Do({ "functionName": "OrderPay", "orgCode": "0004", "contentData": data })
        .then(res => {
            configStore.ispaying.id ++
            console.log('买单确认res :>> ', res);
            obj.itemCode = res.itemCode
            obj.payBatchCode = res.payBatchCode
            tableInfo.value.payBatchCode = res.payBatchCode
            payData.value.push(obj)
            const ls_orderChange = ls.get('orderChange')
            if (ls_orderChange) {
                ls_orderChange.payData = payData.value
                ls.set('orderChange', ls_orderChange)
            }
            form.value.payAmt = ''
            form.value.mountUser = ''
            form.value.mountSex = ''
            form.value.mountMobile = ''
            form.value.suretyUser = ''
        })
        .catch(err=>{
            configStore.ispaying.id ++
        })
}

// 暴露方法给父组件
defineExpose({ handleKeyboardConfirm })
</script>

<style scoped lang="scss">
.form-box {
    margin-top: 20px;

    ::v-deep {
        .el-input__wrapper {
            background: #F9F9F9;
            border-radius: 8px;
            border: 0;
            font-size: 16px;
            color: #999999;
        }
    }

    .salaAmt {
        font-weight: 400;
        font-size: 16px;
        color: #FF3333;
    }

}</style>
